package com.aries.provider.config;

import com.alibaba.dubbo.config.annotation.Service;
import com.mockuai.commons.annotations.MkApiExclude;
import com.mockuai.commons.annotations.MockuaiApi;
import com.mockuai.commons.entitis.MkApiRegisterInfo;
import org.reflections.Reflections;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Author: aries
 * Date: 2018/9/7
 * Description:
 */
@Configuration
public class MkApiRegister implements ApplicationContextAware {

    @PostConstruct
    public void apiRegister() {
        List<MkApiRegisterInfo> infoList = new LinkedList<>();
        Reflections reflections = new Reflections("com.aries");
        Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(RestController.class);
        classSet
                .stream()
                .filter(aClass -> !aClass.isAnnotationPresent(MkApiExclude.class))
                .forEach(aClass -> {
                    Method[] methods = aClass.getMethods();
                    Arrays
                            .stream(methods)
                            .filter(method -> !method.isAnnotationPresent(MkApiExclude.class) && method.isAnnotationPresent(RequestMapping.class))
                            .forEach(method -> {
                                RequestMapping requestMapping = method.getDeclaredAnnotation(RequestMapping.class);
                                MkApiRegisterInfo registerInfo = new MkApiRegisterInfo();
                                MockuaiApi mockuaiApi = method.getDeclaredAnnotation(MockuaiApi.class);
                                registerInfo.setUrl(requestMapping.value()[0]);
                                registerInfo.setMethodName(method.getName());
                                registerInfo.setName(requestMapping.method()[0].name());
                                if (mockuaiApi == null) {
                                    registerInfo.setAuth("3");
                                    registerInfo.setFormat("standard");
                                } else {
                                    registerInfo.setAuth(mockuaiApi.authLevel());
                                    registerInfo.setFormat(mockuaiApi.format());
                                }

                                infoList.add(registerInfo);
                            });
                });

        infoList.forEach(info -> {
            System.out.println(info.toString());
        });

    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        Map<String, Object> map = applicationContext.getBeansWithAnnotation(Service.class);
        map.values().forEach(obj -> {
            Field[] fields = obj.getClass().getSuperclass().getDeclaredFields();
            Arrays.stream(fields).forEach(field -> System.out.println(field.getName()));
        });
    }

}
